# strings are defined with double quotes
# like variables, strings can contain any unicode character
s1 = "The quick brown fox jumps over the lazy dog α,β,γ"
println(s1)
#> The quick brown fox jumps over the lazy dog α,β,γ

# [println](http://julia.readthedocs.org/en/latest/stdlib/base/#Base.println) adds a new line to the end of output
# [print](http://julia.readthedocs.org/en/latest/stdlib/base/#Base.print) can be used if you dont want that:
print("this")
#> this
print(" and")
#> and
print(" that.\n")
#> that.

# chars are defined with single quotes
c1 = 'a'
println(c1)
#> a
# the ascii value of a char can be found with Int():
println(c1, " ascii value = ", Int(c1))
#> a ascii value = 97
println("Int('α') == ", Int('α'))
#> Int('α') == 945

# so be aware that
println(Int('1') == 1)
#> false

# strings can be converted to upper case or lower case:
s1_caps = uppercase(s1)
s1_lower = lowercase(s1)
println(s1_caps, "\n", s1_lower)
#> THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG Α,Β,Γ
#> the quick brown fox jumps over the lazy dog α,β,γ

# sub strings can be indexed like arrays:
# ([show](http://julia.readthedocs.org/en/latest/stdlib/base/#Base.show) prints the raw value)
show(s1[11]); println()
#> 'b'

# or sub strings can be created:
show(s1[1:10]); println()
#> "The quick "

# end is used for the end of the array or string
show(s1[end-10:end]); println()
#> "dog α,β,γ"

# julia allows string [Interpolation](http://julia.readthedocs.org/en/latest/manual/strings/#interpolation):
a = "wolcome"
b = "julia"
println("$a to $b.")
#> wolcome to julia.

# this can extend to evaluate statements:
println("1 + 2 = $(1 + 2)")
#> 1 + 2 = 3

# strings can also be concatenated using the * operator
# using * instead of + isn't intuitive when you start with Julia,
# however [people think it makes more sense](https://groups.google.com/forum/#!msg/julia-users/nQg_d_n0t1Q/9PSt5aya5TsJ)
s2 = "this" * " and" * " that"
println(s2)
#> this and that

# as well as the string function
s3 = string("this", " and", " that")
println(s3)
#> this and that

println("--------")
s = "\u2200 x \u2203 y"
println("s[1]","：",s[1]) #> ∀
println("s[1:4]","：",s[1:4]) #> ∀


for i = firstindex(s):lastindex(s)
   try
       println(s[i])
   catch
       # ignore the index error
   end
end
####迭代字符串更好的方式
for c in s
   println(c)
end
println("length(s)","：",length(s)) #> 7
println("length(s,1,5)","：",length(s,1,5)) #> 3
println("nextind(s,1)","：",nextind(s,1)) #> ∀
println("lastindex(s)","：",lastindex(s)) #> 11
println("string()","：",string(s, ", ", ".\n")) #> ∀ x ∃ y, .
println("findfirst()","：",findfirst(isequal('p'), "xylophone")) #> 5
println("findnext()","：",findnext(isequal('o'), "xylophone", 5)) #> 7
println("occursin()","：",occursin("a", "Xylophon")) #> false
println("repeat()","：",repeat(".:Z:.", 10)) #> .:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:.
println("join()","：",join(["apples", "bananas", "pineapples"], ", ", " and ")) #> apples, bananas and pineapples

println("ncodeunits()","：",ncodeunits("hello world")) #> 11 #获取字符串的代码单元数
println("codeunit()","：",codeunit("hello world", 2)) #> 101 #获取字符串的代码单元值
println("thisind()","：",thisind("hello world", 5)) #>
println("nextind()","：",nextind("hello world", 5, 2)) #>
println("prevind()","：",prevind("hello world", 5, 2)) #>
#println("search()","：",see("Hello World", 'r', 3)) #> 9
println("正则表达式()","：",occursin(r"^\s*(?:#|$)", "# a comment")) #> true
m = match(r"(a|b)(c)?(d)", "ad")
println("match","：", m.match, " | ", m.captures, " | ", m.offset, " | ", m.offsets)
m=match(r"(?<hour>\d+):(?<minute>\d+)","12:45")
println("captures","：", m[:minute], " | ", m[2])
println("replace","：", replace("first second", r"(\w+) (?<agroup>\w+)" => s"\g<agroup> \1"))


println("--------字符串可以包含无效的 UTF-8 代码单元序列")
s = "\xc0\xa0\xe2\x88\xe2|"
foreach(display, s)
for c in s
   println(isvalid(c))
end
#isvalid.(collect(s))

println("--------连接")
greet, whom = "Hello", "world"
println(string(greet, ", ", whom, ".\n"))
println(greet * ", " * whom * ".\n")
println("$greet, $whom.\n")
println("1 + 2 = $(1 + 2)")
println("I have \$100 in my account.\n")

a, b = "\xe2\x88", "\x80"
c = a*b
println("a*b","：", c, " | ", collect.([a, b, c]), " | ", length.([a, b, c])) #> ∀

v = [1,2,3]
println("v: $v")

println("--------常见操作")
println("abracadabra" < "xylophone") #> true
println("abracadabra" == "xylophone") #> false
println("Hello, world." != "Goodbye, world.") #> true
println("1 + 2 = 3" == "1 + 2 = $(1 + 2)") #> true


x = 10
println(r"$x")
println(s"$x")
println(replace("first second", r"(\w+) (?<agroup>\w+)" => s"\g<agroup> \1"))

println(raw"\\ \\\"")#> \\ \"
println("\\ \\\"")#> \\ \"
